package com.dao.impl;

import com.Bean.VoBean.OrderVo;
import com.dao.OrderDao;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoimpl implements OrderDao {

    @Override
    public int insert(OrderVo orderVo) throws SQLException {
        return 0;
    }

    @Override
    public List<OrderVo> queryData(OrderVo orderVo) throws IllegalAccessException, SQLException, InstantiationException {
        String sql="SELECT DISTINCT ltype from room_list o JOIN client c on o.pno=c.pno JOIN room_id i on o.rid=i.rid JOIN room r on r.rtid=i.rtid";
        return JdbcUtil.queryData(sql,OrderVo.class);
    }

    @Override
    public int update(OrderVo orderVo) throws SQLException {
        return 0;
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        return 0;
    }

    @Override
    public List<OrderVo> queryDatalin_status(OrderVo orderVo) throws IllegalAccessException, SQLException, InstantiationException {
        String sql="SELECT DISTINCT lin_status from room_list o JOIN client c on o.pno=c.pno JOIN room_id i on o.rid=i.rid JOIN room r on r.rtid=i.rtid";
        return JdbcUtil.queryData(sql,OrderVo.class);
    }

    @Override
    public List<OrderVo> queryDataout_status(OrderVo orderVo) throws IllegalAccessException, SQLException, InstantiationException {
        String sql="SELECT DISTINCT out_status from room_list o JOIN client c on o.pno=c.pno JOIN room_id i on o.rid=i.rid JOIN room r on r.rtid=i.rtid";
        return JdbcUtil.queryData(sql,OrderVo.class);
    }

    @Override
    public PageUtil<OrderVo> queryDataPage(int currentPgae, int pageSize, OrderVo orderVo) throws IllegalAccessException, SQLException, InstantiationException {
        String sql="SELECT lid,pname,pphone,ltype,r.rtid,rname,o.rid,intime,outtime,lin_status,lsum,out_status,ldate from room_list o JOIN client c on o.pno=c.pno JOIN room_id i on o.rid=i.rid JOIN room r on r.rtid=i.rtid  where 1=1";

        ArrayList values = new ArrayList();

        if (orderVo!=null){
            if (orderVo.getLid()!=null  &&  !"".equals( orderVo.getLid())){
                sql +=" and lid = ? ";
                values.add(orderVo.getLid());
            }
            if (orderVo.getPname()!=null && !"".equals( orderVo.getPname())){
                sql +=" and pname like ? ";
                values.add("%"+orderVo.getPname()+"%");
            }
            if (orderVo.getIntime()!=null && !"".equals( orderVo.getIntime())){
                sql +=" and intime >= ? ";
                values.add(orderVo.getIntime());
            }
            if (orderVo.getOuttime()!=null && !"".equals( orderVo.getOuttime())){
                sql +=" and outtime <= ? ";
                values.add(orderVo.getOuttime());
            }
            if (orderVo.getRtid()!=null && !"".equals( orderVo.getRtid())){
                sql +=" and r.rtid = ? ";
                values.add(orderVo.getRtid());
            }
            if (orderVo.getLtype()!=null && !"".equals( orderVo.getLtype())){
                sql +=" and ltype = ? ";
                values.add(orderVo.getLtype());
            }
            if (orderVo.getLin_status()!=null && !"".equals( orderVo.getLin_status())){
                sql +=" and lin_status = ? ";
                values.add(orderVo.getLin_status());
            }
            if (orderVo.getOut_status()!=null && !"".equals( orderVo.getOut_status())){
                sql +=" and out_status = ? ";
                values.add(orderVo.getOut_status());
            }
            if (orderVo.getLdate()!=null && !"".equals( orderVo.getLdate())){
                sql +=" and ldate < ? ";
                values.add(orderVo.getLdate());
            }

        }

        return JdbcUtil.queryDataByPage(sql,OrderVo.class,currentPgae,pageSize,values.toArray());
    }

    @Override
    public PageUtil<OrderVo> queryDataPageToday(int currentPgae, int pageSize, OrderVo orderVo) throws IllegalAccessException, SQLException, InstantiationException {
        String sql="SELECT lid,pname,pphone,ltype,r.rtid,rname,o.rid,intime,outtime,lin_status,lsum,out_status,ldate from room_list o JOIN client c on o.pno=c.pno JOIN room_id i on o.rid=i.rid JOIN room r on r.rtid=i.rtid  where 1=1";

        ArrayList values = new ArrayList();

        if (orderVo!=null){
            if (orderVo.getLid()!=null  &&  !"".equals( orderVo.getLid())){
                sql +=" and lid = ? ";
                values.add(orderVo.getLid());
            }
            if (orderVo.getPname()!=null && !"".equals( orderVo.getPname())){
                sql +=" and pname like ? ";
                values.add("%"+orderVo.getPname()+"%");
            }
            if (orderVo.getIntime()!=null && !"".equals( orderVo.getIntime())){
                sql +=" and intime = ? ";
                values.add(orderVo.getIntime());
            }
            if (orderVo.getOuttime()!=null && !"".equals( orderVo.getOuttime())){
                sql +=" and outtime = ? ";
                values.add(orderVo.getOuttime());
            }
            if (orderVo.getRtid()!=null && !"".equals( orderVo.getRtid())){
                sql +=" and r.rtid = ? ";
                values.add(orderVo.getRtid());
            }
            if (orderVo.getLtype()!=null && !"".equals( orderVo.getLtype())){
                sql +=" and ltype = ? ";
                values.add(orderVo.getLtype());
            }
            if (orderVo.getLin_status()!=null && !"".equals( orderVo.getLin_status())){
                sql +=" and lin_status = ? ";
                values.add(orderVo.getLin_status());
            }
            if (orderVo.getOut_status()!=null && !"".equals( orderVo.getOut_status())){
                sql +=" and out_status = ? ";
                values.add(orderVo.getOut_status());
            }
            if (orderVo.getLdate()!=null && !"".equals( orderVo.getLdate())){
                sql +=" and TO_DAYS(ldate) = TO_DAYS("+"?"+")";
                values.add(orderVo.getLdate());
            }

        }

        return JdbcUtil.queryDataByPage(sql,OrderVo.class,currentPgae,pageSize,values.toArray());
    }
}
